var selectOption = "<option value='0'>选择</option>";
$("select").prepend(selectOption);
var jsonProduct = jQuery.parseJSON(product);

$(function() {
    $(".editor").blur(function() {
        $(this).btOff();
    });
    $("#items tbody tr td select.product").change(function() {
        var my = $(this);
        var key = eval(my.val());
        var nextId = my.next().attr("id");
        if (my.next().length == 0) {
            return true;
        }
        var data = my.data("products");
        if (data == undefined) {
            my.data("products", jsonProduct);
            data = jsonProduct;
        }
        try {
            var next = my.next();
            while (next.length != 0) {
                dwr.util.removeAllOptions(next.attr("id"));
                next.prepend(selectOption);
                next = next.next();
            }
        } catch(e) {
        }
        $.map(data, function(o) {
            if (o.id == key) {
                dwr.util.addOptions(nextId, o.children, 'id', 'name');
                my.next().data("products", o.children);
            }
        });
    });
    $("#province").change(function() {
        var key = eval($(this).val());
        dwr.util.removeAllOptions("city");
        var json = jQuery.parseJSON(city);
        $(this).next().prepend(selectOption);
        dwr.util.addOptions("city", json[key], 'id', 'name');
    });
    $("select").each(function(i) {
        $(this).val($(this).attr("accesskey"));
        $(this).trigger("change");
    });
});

function checkTable(expired) {
    /*if (!expired) {
        alert("资源信息未过期，不能再次发布。");
        return false;
    }*/
    var result = false;
    $(".editor").each(function(i) {
        var val = $(this).val();
        var lang = $(this).attr("lang");
        var title = $(this).attr("title");
        var pass = true;
        if ("string" == lang) {
            if (val.isEmpty()) pass = false;
        } else if ("select" == lang) {
            if (val == 0) pass = false;
        } else if ("int" == lang) {
            if (val <= 0) pass = false;
            if (!val.isInteger()) pass = false;
        }
        if (!pass) {
            $(this).bt(title, { trigger: 'none', clickAnywhereToClose:false,positions: 'bottom',killTitle:false});
            $(this).btOn();
            $(this).focus();
            result = false;
            return false;
        }
        result = true;
        return true;
    });
    return result;
}

function addRow() {
    var clone = $("#items tbody tr:first").clone(true);
    clone.find("select").val(0).removeAttr("accesskey");
    clone.find("input").val("");
    $("#items tbody").append(clone);
    resetCount();
}

function resetCount() {
    var size = $("#items tbody tr").size();
    $("#items tbody tr").each(function(i) {
        $(this).attr("id", "tr" + i);
        $(this).find("select").add("input").each(function() {
            var name = $(this).attr("name");
            $(this).attr("name", name.replace(/\[\d+\]/g, "[" + i + "]"));
            var id = $(this).attr("id");
            $(this).attr("id", id.replace(/-\d+/g, "-" + i));
        });
        $(this).find("a.delete").attr("href", "javascript:deleteRow(" + i + ");");
    });
    $("#count").val(size);
}

function deleteRow(index) {
    var size = $("#items tbody tr").size();
    if (size == 1) {
        alert("不能删除最后一条");
    } else {
        $("#tr" + index).remove();
    }
    resetCount();
}